package com.zjj.wangluo.upload;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @author zhoujunjie
 * @desc TODO
 * @date
 */
public class UploadServer {

    public static void main(String[] args) {

        try(
                //通过ServerSocket，接入的Socket套接字读消息
                ServerSocket ss = new ServerSocket(8888);
                Socket socket = ss.accept();
                BufferedInputStream bis = new BufferedInputStream(socket.getInputStream());
                //将图片信息写至服务器磁盘上
                BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("D:\\a\\new.jpg"));


                //回传消息
                PrintWriter pw = new PrintWriter(socket.getOutputStream());
        ) {
            System.out.println("---------- UploadServer启动 -----------");
            //上传服务器  边读编写
            byte[] buffer = new byte[1024];
            int len = 0;
            while ((len = bis.read(buffer))!=-1){
                bos.write(buffer , 0 , len);
            }
            //写完刷新
            bos.flush();

            //写回上传成功的消息
            pw.println("图片上传成功");
            pw.flush();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }

}
